package util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import egg.Egg;
import env.Application;
import env.Env;
import frog.Frog;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class EggTool {

    public static final boolean JSON_FILE_FORMAT = false; // JSON is slow but easier to debug


    public static void loadEggs(Env env){
        boolean errorfound = false;
        if (JSON_FILE_FORMAT) {
            String eggsString = FrogFileUtils.readFile(Application.CLASSPATH + "eggs.json", "utf-8");
            if (eggsString != null) {
                List<JSONObject> jsonEggs = (List<JSONObject>) JSON.parse(eggsString);
                env.eggs = new ArrayList<Egg>();
                for (JSONObject json : jsonEggs) {
                    Egg egg = json.toJavaObject(Egg.class);
                    env.eggs.add(egg);
                }
                System.out.println(
                        "Loaded " + env.eggs.size() + " eggs from file '" + Application.CLASSPATH + "eggs.json" + "'.");
            } else
                errorfound = true;
        } else {
            try {
                FileInputStream eggsFile = new FileInputStream(Application.CLASSPATH + "eggs.ser");
                ObjectInputStream eggsInputStream = new ObjectInputStream(eggsFile);
                env.eggs = (List<Egg>) eggsInputStream.readObject();
                System.out.println(
                        "Loaded " + env.eggs.size() + " eggs from file '" + Application.CLASSPATH + "eggs.ser" + "'.");
                eggsInputStream.close();
            } catch (Exception e) {
                errorfound = true;
            }
        }
        if(errorfound) {

            System.out.println("没有egg文件，创建全新的egg");
            env.eggs = new ArrayList<Egg>();
            for (int i = 0; i < env.EGG_QTY; i++) {
                env.eggs.add(Egg.createBrandNewEgg());

            }
        }

    }

    //截取能量最高的前四分之一forg保存下蛋
    public static void layEgg(Env env) {
        sortFrogsOrderByDesc(env);
        System.out.print("First frog energy=" + env.frogs.get(0).energy);
        System.out.print(",  Last frog energy=" + env.frogs.get(env.frogs.size() - 1).energy + ",  ");
        for (Frog frog : env.frogs) {
            System.out.print(frog.energy + ",");
        }
        System.out.println();
        try {
            List<Egg> newEggs = new ArrayList<Egg>();
            for (int i = 0; i < env.EGG_QTY; i++)
                newEggs.add(env.frogs.get(i).layEgg());
            if (JSON_FILE_FORMAT) {
                String newEggsString = JSON.toJSONString(newEggs);
                FrogFileUtils.writeFile(Application.CLASSPATH + "eggs.json", newEggsString, "utf-8");
            } else {
                FileOutputStream fo = new FileOutputStream(Application.CLASSPATH + "eggs.ser");
                ObjectOutputStream so = new ObjectOutputStream(fo);
                so.writeObject(newEggs);
                so. close();
            }
            env.eggs = newEggs;
            System.out
                    .println("Saved " + env.eggs.size() + " eggs to file '" + Application.CLASSPATH + "eggs.ser" + "'");
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    //按照energy大小对当前forg进行排序
    static void sortFrogsOrderByDesc(Env env){
        Collections.sort(env.frogs, new Comparator<Frog>() {
            @Override
            public int compare(Frog o1, Frog o2) {
                if(o1.energy>o2.energy)
                    return -1;
                else if(o1.energy==o2.energy)
                    return 0;
                else
                    return 1;
            }
        });
    }
}
